import 'dart:math';

import 'package:flutter/material.dart';

class OptimizeTest extends StatefulWidget {
  @override
  _OptimizeTestState createState() => _OptimizeTestState();
}

class _OptimizeTestState extends State<OptimizeTest>
    with SingleTickerProviderStateMixin {
  AnimationController _controller;

  int nums1 = 0;
  int nums2 = 0;

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(vsync: this);
  }

  @override
  void dispose() {
    super.dispose();
    _controller.dispose();
  }

  @override
  Widget build(BuildContext context) {
    print('class-build');
    return Scaffold(
      appBar: AppBar(
        title: Text('OptimizeTest'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            ShaderMask(
              shaderCallback: (Rect bounds) {
                return RadialGradient(
                  center: Alignment.topLeft,
                  radius: 1.0,
                  colors: <Color>[Colors.yellow, Colors.deepOrange],
                  tileMode: TileMode.mirror,
                ).createShader(bounds);
              },
              blendMode: BlendMode.srcATop,
              child: Text(
                '一枚有态度的程序员',
                style: TextStyle(fontSize: 30),
              ),
            ),
            Buttonclick(
              color: Colors.orange,
              key: ValueKey(11),
            ),
            Buttonclick(
              color: Colors.green,
              key: ValueKey(22),
            ),
            RepaintBoundary(
              child: Container(
                width: 100,
                height: 100,
                color: Colors.pink,
                child: TextButton(
                    onPressed: () {
                      nums1++;
                      setState(() {});
                    },
                    child: Text(
                      nums1.toString(),
                      style: TextStyle(color: Colors.black, fontSize: 40),
                    )),
              ),
            ),
            Container(
              width: 100,
              height: 100,
              color: Colors.red,
              child: TextButton(
                onPressed: () {
                  nums2++;
                },
                child: Text(
                  nums2.toString(),
                  style: TextStyle(color: Colors.black, fontSize: 40),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

class Buttonclick extends StatefulWidget {
  final Color color;

  const Buttonclick({Key key, this.color}) : super(key: key);
  @override
  _ButtonclickState createState() => _ButtonclickState();
}

class _ButtonclickState extends State<Buttonclick> {
  int nums = 0;

  @override
  void deactivate() {
    print('deactivate');
    super.deactivate();
  }

  @override
  void dispose() {
    print('dispose');
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    print('Buttonclick-- build -- ${widget.key}');
    return Container(
      width: 100,
      height: 100,
      color: widget.color,
      child: TextButton(
          onPressed: () => setState(() {
                nums++;
              }),
          child: Text(
            nums.toString(),
            style: TextStyle(color: Colors.black, fontSize: 40),
          )),
    );
  }
}
